﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections.ObjectModel;
using produccion.AccesoDatos;

namespace produccion.modelo
{
    public class MFacturacion : ObservableObject
    {
        public class facturacionVarios : ObservableObject
        {
            #region Propiedades
            
                private string _strCodigo;
                public string strCodigo
                {
                    get { return _strCodigo; }
                    set { _strCodigo = value; }
                }

                private string _strDescripcion;
                public string strDescripcion
                {
                    get { return _strDescripcion; }
                    set { _strDescripcion = value; }
                }

                private double _numCantidad;
                public double numCantidad
                {
                    get { return _numCantidad; }
                    set { _numCantidad = value; }
                }

                private double _numValor;
                public double numValor
                {
                    get { return _numValor; }
                    set { _numValor = value; }
                }

                private double _numIva;
                public double numIva
                {
                    get { return _numIva; }
                    set { _numIva = value; }
                }

                private double _numTotalIva;
                public double numTotalIva
                {
                    get { return _numTotalIva; }
                    set { _numTotalIva = value; }
                }

                private double _numDescuento;
                public double numDescuento
                {
                    get { return _numDescuento; }
                    set { _numDescuento = value; }
                }

                private double _numSubtotal;
                public double numSubtotal
                {
                    get { return _numSubtotal; }
                    set { _numSubtotal = value; }
                }

            #endregion
        }

        public class clientes : ObservableObject
        {
            MDOgeneral mod = new MDOgeneral();

            #region propiedades
                private string _CODCLI_CLI;
                public string CODCLI_CLI
                {
                    get { return _CODCLI_CLI; }
                    set { _CODCLI_CLI = value; }
                }

                private string _NOMCLI_CLI;
                public string NOMCLI_CLI
                {
                    get { return _NOMCLI_CLI; }
                    set { _NOMCLI_CLI = value; }
                }

                private Int32 _NROORDENES;
                public Int32 NROORDENES
                {
                    get { return _NROORDENES; }
                    set { _NROORDENES = value; }
                }

                private double _TOTARTICULOS;
                public double TOTARTICULOS
                {
                    get { return _TOTARTICULOS; }
                    set { _TOTARTICULOS = value; }
                }

                private double _VALORTOTAL;
                public double VALORTOTAL
                {
                    get { return _VALORTOTAL; }
                    set { _VALORTOTAL = value; }
                }
            #endregion

            #region Metodos
                public List<MFacturacion.clientes> consultar()
                {
                    try
                    {
                        string query = "select c.codcli_cli, c.nomcli_cli, count(distinct b.nroord_eod) as nroOrdenes, sum(a.canpro_dod) as totArticulos, sum(a.canpro_dod*a.prepro_dod) as valorTotal from detordd a inner join encordd b on a.nroord_dod=b.nroord_eod inner join maeclip c on b.codcli_eod=c.codcli_cli where b.estado_eod='L' group by c.codcli_cli, c.nomcli_cli ";
                        DataTable dt = mod.consulta(query);
                        List<MFacturacion.clientes> lista = new List<MFacturacion.clientes>();
                        foreach (DataRow dr in dt.Rows)
                        {
                            lista.Add(new MFacturacion.clientes
                            {
                                CODCLI_CLI = dr["CODCLI_CLI"].ToString(),
                                NOMCLI_CLI = dr["NOMCLI_CLI"].ToString(),
                                NROORDENES = Convert.ToInt32(dr["NROORDENES"]),
                                TOTARTICULOS = Convert.ToDouble(dr["TOTARTICULOS"]),
                                VALORTOTAL = Convert.ToDouble(dr["VALORTOTAL"])

                            });
                        }
                        return lista;
                    }
                    catch (Exception ex) { throw ex; }
                }
            #endregion
        }

        public class detalleOrdenes : ObservableObject
        {
            MDOgeneral mod = new MDOgeneral();

            #region Propiedades
                private string _NROORD_EOD;
                public string NROORD_EOD
                {
                    get { return _NROORD_EOD; }
                    set { _NROORD_EOD = value; }
                }
                private string _FECORD_EOD;
                public string FECORD_EOD
                {
                    get { return _FECORD_EOD; }
                    set { _FECORD_EOD = value; }
                }
                private Int32 _TOTARTICULOS;
                public Int32 TOTARTICULOS
                {
                    get { return _TOTARTICULOS; }
                    set { _TOTARTICULOS = value; }
                }
                private double _VALORTOTAL;
                public double VALORTOTAL
                {
                    get { return _VALORTOTAL; }
                    set { _VALORTOTAL = value; }
                }
                private bool _logIncluir;
                public bool logIncluir
                {
                    get { return _logIncluir; }
                    set { _logIncluir = value; }
                }
                private string _CODCLI_EOD;
                public string CODCLI_EOD
                {
                    get { return _CODCLI_EOD; }
                    set { _CODCLI_EOD = value; }
                }
                private string _strDescripcion;
                public string strDescripcion
                {
                    get { return _strDescripcion; }
                    set { _strDescripcion = value; }
                }
                private bool _logFacturar;
                public bool logFacturar
                {
                    get { return _logFacturar; }
                    set { _logFacturar = value; }
                }
                private string _strTipoOrden;
                public string strTipoOrden
                {
                    get { return _strTipoOrden; }
                    set { _strTipoOrden = value; }
                }
                private Int32 _numPedido;
                public Int32 numPedido
                {
                    get { return _numPedido; }
                    set { _numPedido = value; }
                }
            #endregion

            #region Metodos
                public List<MFacturacion.detalleOrdenes> consultar()
                {
                    try
                    {
                        string query = "select b.codcli_eod, b.nroord_eod, ''||b.fecord_eod||'' as fecord_eod, sum(a.canpro_dod) as totArticulos, sum(a.canpro_dod*a.prepro_dod) as valorTotal, 0 as logIncluir, c.strDescripcion, nvl(b.tipord_eod,'P') as tipord_eod, nvl((select  distinct nroped_eop from encordp where nroord_eop in (select distinct nroordp_ood from ordordd where nroordd_ood = b.nroord_eod) and rownum = 1),0) as numPedido from detordd a inner join encordd b on a.nroord_dod=b.nroord_eod left join (select distinct nroord_eod, 'Esta Ordene tiene items sin precio' as strDescripcion from detordd a inner join encordd b on a.nroord_dod=b.nroord_eod where estado_eod = 'L' and prepro_dod=0 and codcli_eod = '" + CODCLI_EOD + "' ) c on b.nroord_eod = c.nroord_eod where estado_eod = 'L' and b.codcli_eod = '" + CODCLI_EOD + "' group by b.codcli_eod, b.nroord_eod, b.fecord_eod, c.strDescripcion, b.tipord_eod, 0 order by b.fecord_eod desc ";
                        DataTable dt = mod.consulta(query);
                        List<MFacturacion.detalleOrdenes> lista = new List<detalleOrdenes>();
                        foreach (DataRow dr in dt.Rows)
                        {
                            lista.Add(new detalleOrdenes 
                            { 
                                NROORD_EOD = dr["NROORD_EOD"].ToString(),
                                FECORD_EOD = dr["FECORD_EOD"].ToString(),
                                TOTARTICULOS = Convert.ToInt32(dr["TOTARTICULOS"]),
                                VALORTOTAL = Convert.ToDouble(dr["VALORTOTAL"]),
                                logIncluir = Convert.ToBoolean(dr["logIncluir"]),
                                CODCLI_EOD = dr["CODCLI_EOD"].ToString(),
                                strDescripcion = dr["strDescripcion"].ToString(),
                                logFacturar = true,
                                strTipoOrden = dr["TIPORD_EOD"].ToString(),
                                numPedido = Convert.ToInt32(dr["numPedido"])
                            });
                        }
                        return lista;
                    }
                    catch (Exception ex) { throw ex; }
                }

                public DataTable consultaInfFactura()
                {
                    try
                    {
                        string query = "select * from controlp where id_con = 'NRO' and cod_con = 'FAC' union select * from controlp where id_con = 'RES' and cod_con = 'ART' union select * from controlp where id_con = 'TAR' and cod_con = 'IVA' union select 'BAS' as ID_CON, 'RFE' as COD_CON, TO_CHAR(BASRTE_CFC) AS BASRTE_CFC, 'BASE RETENCION' AS des_con from ccpfacp WHERE CODCCP_CFC = '01' union select 'POR' as ID_CON, 'RFE' as COD_CON, TO_CHAR(PORRTE_CFC) AS PORRTE_CFC, 'PORCENTAJE RETENCION' AS des_con from ccpfacp WHERE CODCCP_CFC = '01' union select * from controlp where id_con = 'FAC' and cod_con = 'FIN' ";
                        return mod.consulta(query);
                    }
                    catch (Exception ex) { throw ex; }
                }

                public DataTable consultaConsecutivoFactura(string strEmp)
                {
                    try
                    {
                        string query = "";
                        if (strEmp == "A")
                            query = "select * from controlp where id_con = 'NRO' and cod_con = 'FAC' ";
                        else
                            query = "select * from controlp where id_con = 'NRO' and cod_con = 'FPE' ";
                        return mod.consulta(query);
                    }
                    catch (Exception ex) { throw ex; }
                }

                public DataTable consultaConsecutivoNC(string strEmp)
                {
                    try
                    {
                        string query = "";
                        if (strEmp == "A")
                            query = "select * from controlp where id_con = 'NRO' and cod_con = 'NC' ";
                        else
                            query = "select * from controlp where id_con = 'NRO' and cod_con = 'NPE' ";
                        return mod.consulta(query);
                    }
                    catch (Exception ex) { throw ex; }
                }

                public DataTable consultaConsecutivoND(string strEmp)
                {
                    try
                    {
                        string query = "";
                        if (strEmp == "A")
                            query = "select * from controlp where id_con = 'NRO' and cod_con = 'DEV' ";
                        else
                            query = "select * from controlp where id_con = 'NRO' and cod_con = 'DPE' ";
                        return mod.consulta(query);
                    }
                    catch (Exception ex) { throw ex; }
                }

                public DataTable consultaOrdenItemSinPrecio(string strNumeroOrden)
                {
                    try
                    {
                        string query = "select a.codref_dod, b.nomref_ref, a.codcol_dod, c.nomcol_col, a.codtal_dod, a.canpro_dod from detordd a inner join maeref b on a.codref_dod=b.codref_ref inner join maecol c on a.codcol_dod=codcol_col where a.nroord_dod='" + strNumeroOrden + "' and a.prepro_dod=0";
                        return mod.consulta(query);
                    }
                    catch (Exception ex) { throw ex; }
                }

            #endregion
        }

        public class devoluciones : ObservableObject
        {
            MDOgeneral mod = new MDOgeneral();
            private ProcesoProduccionRepositorio _repositorio = new ProcesoProduccionRepositorio();

            #region Propiedades
            private string _strOrden;
            public string strOrden
            {
                get { return _strOrden; }
                set { _strOrden = value; }
            }

            private string _strRemision;
            public string strRemision
            {
                get { return _strRemision; }
                set { _strRemision = value; }
            }

            private string _strFecha;
            public string strFecha
            {
                get { return _strFecha; }
                set { _strFecha = value; }
            }

            private string _strNombreReferencia;
            public string strNombreReferencia
            {
                get { return _strNombreReferencia; }
                set { _strNombreReferencia = value; }
            }

            private string _strNombreColor;
            public string strNombreColor
            {
                get { return _strNombreColor; }
                set { _strNombreColor = value; }
            }

            private string _strTalla;
            public string strTalla
            {
                get { return _strTalla; }
                set { _strTalla = value; }
            }

            private Int32 _numPrecio;
            public Int32 numPrecio
            {
                get { return _numPrecio; }
                set { _numPrecio = value; }
            }
            
            private bool _logIncluir;
            public bool logIncluir
            {
                get { return _logIncluir; }
                set { _logIncluir = value; }
            }

            private string _strNumeroReparacion;
            public string strNumeroReparacion
            {
                get { return _strNumeroReparacion; }
                set { _strNumeroReparacion = value; }
            }
            
            #endregion

            #region Metodos
            public List<MFacturacion.devoluciones> consultar(string strCliente, string strTipo)
            {
                try
                {
                    DataTable dt = _repositorio.consultarDevolucionesPendientes(strCliente, strTipo);
                    List<MFacturacion.devoluciones> lista = new List<MFacturacion.devoluciones>();
                    foreach (DataRow dr in dt.Rows)
                    {
                        lista.Add(new MFacturacion.devoluciones
                        {
                            strOrden = dr["NROORD_TMP"].ToString(),
                            strRemision = dr["NROREM_TMP"].ToString(),
                            strFecha = Convert.ToDateTime(dr["FECHA_TMP"]).ToString("dd/MM/yyyy"),
                            strNombreReferencia = dr["NOMREF_REF"].ToString(),
                            strNombreColor = dr["NOMCOL_TMP"].ToString(),
                            strTalla = dr["CODTAL_TMP"].ToString(),
                            numPrecio = Convert.ToInt32(dr["PRECIO_TMP"]),
                            strNumeroReparacion = dr["NROREP_TMP"].ToString(),
                            logIncluir = true
                        });
                    }
                    return lista;
                }
                catch (Exception ex) { throw ex; }
            }

           

            #endregion
        }



        public class anularfactura : ObservableObject
        {
            MDOgeneral mod = new MDOgeneral();
            private ProcesoProduccionRepositorio _repositorio = new ProcesoProduccionRepositorio();

            #region Propiedades

            private Int32 _numConsecutivo;
            public Int32 numConsecutivo
            {
                get { return _numConsecutivo; }
                set { _numConsecutivo = value; }
            }

            private string _strReferencia;
            public string strReferencia
            {
                get { return _strReferencia; }
                set { _strReferencia = value; }
            }

            private string _strNombreReferencia;
            public string strNombreReferencia
            {
                get { return _strNombreReferencia; }
                set { _strNombreReferencia = value; }
            }

            private double _numCantidad;
            public double numCantidad
            {
                get { return _numCantidad; }
                set { _numCantidad = value; }
            }

            private double _numPrecio;
            public double numPrecio
            {
                get { return _numPrecio; }
                set { _numPrecio = value; }
            }

            private double _numIva;
            public double numIva
            {
                get { return _numIva; }
                set { _numIva = value; }
            }

            private double _numDescuento;
            public double numDescuento
            {
                get { return _numDescuento; }
                set { _numDescuento = value; }
            }

            #endregion

            #region Metodos
            public List<MFacturacion.anularfactura> consultarDetalle(string strNroFactura)
            {
                try
                {
                    string query = "select consec_dfa, codref_dfa, nomref_dfa, NVL(canref_dfa,0) as canref_dfa, NVL(preref_dfa,0) as preref_dfa, NVL(ivaref_dfa,0) as ivaref_dfa, NVL(dtoref_dfa,0) as dtoref_dfa from detfacp where nrofac_dfa = '" + strNroFactura.Trim() + "'";
                    DataTable dt = mod.consulta(query);
                    List<MFacturacion.anularfactura> lista = new List<MFacturacion.anularfactura>();

                    foreach (DataRow dr in dt.Rows)
                    {
                        lista.Add(new MFacturacion.anularfactura
                        {
                            numConsecutivo = Convert.ToInt32(dr["CONSEC_DFA"]),
                            strReferencia = dr["CODREF_DFA"].ToString(),
                            strNombreReferencia = dr["NOMREF_DFA"].ToString(),
                            numCantidad = Convert.ToDouble(dr["CANREF_DFA"]),
                            numPrecio = Convert.ToDouble(dr["PREREF_DFA"]),
                            numIva = Convert.ToDouble(dr["IVAREF_DFA"]),
                            numDescuento = Convert.ToDouble(dr["DTOREF_DFA"])
                        });
                    }
                    return lista;
                }
                catch (Exception ex) { throw ex; }
            }

            public DataTable consultaEncabezado(string strNroFactura)
            {
                try
                {
                    string query = "select NROFAC_EFA, CODCLI_EFA, FECFAC_EFA, VTOFAC_EFA, VALFAC_EFA, IVAFAC_EFA, RTEFAC_EFA, DTOFAC_EFA, ABOFAC_EFA, CONCEP_EFA, ESTADO_EFA, FLETE_EFA, TIPFAC_EFA, EMPRESA_EFA, DETALLE_EFA from encfacp where nrofac_efa = '" + strNroFactura + "'";
                    return mod.consulta(query);
                }
                catch (Exception ex) { throw ex; }
            }

            #endregion
        }

    }
}
